class Solution:
    def balancedString(self, s: str) -> int:
        m = {'Q': 0, 'W': 0, 'E': 0, 'R': 0}
        for string in s:
            m[string] += 1
        res = len(s)
        i = 0
        n = len(s) // 4
        for j in range(len(s)):
            m[s[j]] -= 1
            while i < len(s) and (m['Q'] <= n and m['W'] <= n and m['E'] <= n
                                  and m['R'] <= n):
                res = min(res, j - i + 1)
                m[s[i]] += 1
                i += 1

        return res
